﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using MapCenterSytem.Process;
using MapCenterSystem.DataInfo;
using System.Collections.Generic;
using MapCenterSystem.Utility;

using System.Threading;
using System.Resources;
using System.Globalization;

namespace MobiMap.Admin
{
    public partial class ResultService : System.Web.UI.Page
    {
        ResourceManager rm;
        CultureInfo ci;

        IList<PlaceInfo> lstService;
        string keyword = string.Empty;

        string strResults = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Language
            SetLanguage();

            if (!IsPostBack)
            {
                if (Request.QueryString["keyword"] != null)
                {
                    keyword = Request.QueryString["keyword"];
                    TextBox txtKeyword = (TextBox)Master.FindControl("txtSearchService");
                    txtKeyword.Text = keyword;

                    Cache.Remove("mobimap_listService");
                    BindListService(keyword);
                }
                
            }
                       
            //Tab active
            Label lbService = (Label)Master.FindControl("tabService");
            lbService.CssClass = "tab_active";            
        }

        public void SetLanguage()
        {
            rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
            if (Request.Cookies["language"] != null)
            {
                string language = Request.Cookies["language"].Value;
                Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
                LoadString(Thread.CurrentThread.CurrentCulture);

                return;
            }

            //Defaut language is Vietnamese
            Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
            ci = Thread.CurrentThread.CurrentCulture;
            LoadString(ci);
        }

        public void LoadString(CultureInfo ci)
        {
            strResults = rm.GetString("Admin_Place_Resulst", ci);
        }
        

        public void BindListService(String keyword)
        {
            keyword = StringHelper.GetSearchString(keyword, AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["abbr_file"]);
            lstService = new List<PlaceInfo>();
            
            if (Cache["mobimap_listService"] == null)
            {
                
                lstService = PlaceEx.GetPlaceByKeyword(keyword);
                Cache["mobimap_listService"] = lstService;
            }
            else
            {
                lstService = (IList<PlaceInfo>)Cache["mobimap_listService"];
            }

            gvService.DataSource = lstService;
            gvService.DataBind();
            
            lbNumOfResults.Text = lstService.Count + " " + strResults;
        }

        public void lbtPlace_Click(Object sender, EventArgs e)
        {
            LinkButton link = (LinkButton)sender;
            Control selectedRow = (Control)link.Parent;            

            Label lbID = (Label)selectedRow.FindControl("lbID");
            int ID = int.Parse(lbID.Text);            
            Response.Redirect("ServiceDetail.aspx?ID=" + ID);
        }
        
        protected void gvService_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvService.PageIndex = e.NewPageIndex;
            BindListService(keyword);
        }

        
    }
}
